package com.example.demo;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.joda.time.DateTime;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.junit.jupiter.api.Test;

public class Demo01 {
    //	@Test
    public void testName() throws Exception {
//		Thread.g.getContextClassLoader().getResource("");
        String kString = Thread.currentThread().getContextClassLoader().getResource("").getFile();
        System.out.println(kString);

        File createTempFile = File.createTempFile("grq", "daizi");
        System.out.println(createTempFile);

        System.out.println("12".hashCode());
    }

    //	@Test
    public void downPic() {

        // 设置连接超时的时间
        try {
            System.out.println(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
            URL url = new URL("http://127.0.0.1/1.jpg");
            URLConnection openConnection = url.openConnection();
//			openConnection.setConnectTimeout(1000);
//			openConnection.setReadTimeout(1000);
            InputStream openStream = url.openStream();

            FileUtils.copyInputStreamToFile(openStream, new File("d://11.jpg"));
            System.out.println("Demo01.downPic()");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));

    }

    //	@Test
    public void downPicJsop() throws IOException {
        URL url = new URL("http://127.0.0.1/1.jpg");
        String urlsString = "http://127.0.0.1:8848/test/img/1.png";

        Connection ref = Jsoup.connect(urlsString).ignoreContentType(true).referrer(urlsString);
        Response execute = ref.execute();
        BufferedInputStream bodyStream = execute.bodyStream();
        // https://blog.csdn.net/wangcunhuazi/article/details/46287709

        InputStream openStream = url.openStream();
        FileUtils.copyInputStreamToFile(openStream, new File("d://11.jpg"));
        System.out.println("Demo01.downPic()");

    }

    @Test
    public void downPicHttpClient() {
        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost method = new HttpPost("http://127.0.0.1:8848/test/img/1.png");
        HttpResponse result;
        try {
            result = httpClient.execute(method);

            if (result.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = result.getEntity();

                InputStream in = entity.getContent();
                FileUtils.copyInputStreamToFile(in, new File("d://1234.jpg"));

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void stringformat() {
        String format = String.format("redis://%s:%s", "127.0.0.1", "9090");
        System.out.println(format);

    }


}
